YZYZ.Admin						= {};
YZYZ.Admin.Region				= {};
YZYZ.Admin.Region.gridPanel 	= null;
YZYZ.Admin.Region.store 		= null;
YZYZ.Admin.Region.columnModel   = null;
YZYZ.Admin.Region.newRegionWin  = null;
YZYZ.Admin.Region.newRegionForm = null;
YZYZ.Admin.Region.newRegionName = null;
YZYZ.Admin.Region.newRecord = Ext.data.Record.create([
	{ name: 'id', type: 'string'},
	{ name: 'name', type: 'string'},
	{ name: 'available', type: 'boolean'},
	{ name: 'level', type: 'int'},
	{ name: 'description', type: 'string'}
]);
YZYZ.Admin.Region.store = new Ext.data.Store({ 
	proxy: new Ext.data.HttpProxy ({
        url: document.getElementById("regionUrl").value
    }),
    reader: new Ext.data.JsonReader({
    	root			: "data",
    	totalProperty	: "sum"
	}, [
        {name:'id'},
        {name:'name'},
        {name:'description'},
        {name:'level', type:'int'},
        {name:'available',type:'boolean'}
    ]),
    remoteSort: false
});
YZYZ.Admin.Region.disableConfirm = function(regionId){
	Ext.MessageBox.show({
		title:'确认',
		msg:'您确定要禁止此组吗？',
		buttons:Ext.MessageBox.OKCANCEL,
		fn:function(btn){YZYZ.Admin.Region.handleDisable(btn,regionId);}
	});
};
YZYZ.Admin.Region.handleDisable = function(btn,regionId){
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("disableRegionUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			regionId 	:  regionId
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("禁止成功");
				YZYZ.Admin.Region.gridPanel.getSelectionModel().getSelected().set("available",false);
				YZYZ.Admin.Region.gridPanel.getStore().commitChanges();
				
			}
			else {
				YZYZ.Curtain.alert(result.message);
				flag = false;
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序错误或网络故障");
			flag = false;
		}
	});
};
YZYZ.Admin.Region.enableConfirm = function(regionId){
	Ext.MessageBox.show({
		title:'确认',
		msg:'您确定要激活此组吗？',
		buttons:Ext.MessageBox.OKCANCEL,
		fn:function(btn){YZYZ.Admin.Region.handleEnable(btn,regionId);}
	});
};
YZYZ.Admin.Region.handleEnable = function(btn,regionId){
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("enableRegionUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			regionId 	:  regionId
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("激活成功");
				YZYZ.Admin.Region.gridPanel.getSelectionModel().getSelected().set("available",true);
				YZYZ.Admin.Region.gridPanel.getStore().commitChanges();
				
			}
			else {
				YZYZ.Curtain.alert(result.message);
				flag = false;
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序错误或网络故障");
			flag = false;
		}
	});
};
YZYZ.Admin.Region.deleteConfirm = function(regionId){
	Ext.MessageBox.show({
		title:'确认',
		msg:'若删除，则它所包含的景区也将会被删除。您确定要删除此地区或者组吗？',
		buttons:Ext.MessageBox.OKCANCEL,
		fn:function(btn){YZYZ.Admin.Region.handleDelete(btn,regionId);}
	});	
};
YZYZ.Admin.Region.handleDelete = function(btn,regionId){
	if(btn !='ok'){
		return;
	} 
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("deleteUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			regionId 	:  regionId
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("删除成功");
				YZYZ.Admin.Region.gridPanel.getStore().remove(YZYZ.Admin.Region.gridPanel.getSelectionModel().getSelected());
				YZYZ.Admin.Region.gridPanel.getStore().commitChanges();
				
			}
			else {
				YZYZ.Curtain.alert(result.message);
				flag = false;
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序错误或网络故障");
			flag = false;
		}
	});	
};
YZYZ.Admin.Region.opRender = function(value, metaData, record, rowIndex, colIndex, store){
	var html = "<div class='btnCotainer'>";
	if(value == false) {
		html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Region.enableConfirm("+record.get('id')+")' class='btnDisabled divDisabled'>激活ֹ</button></div>"
	} else {
		html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Region.disableConfirm("+record.get('id')+")' class='btnDisabled divEnabled'>禁止ֹ</button></div>"
	}
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Region.showEditWin()' class='btnEdit'>编辑</button></div>";
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Region.showEditWin()' class='btnEdit'>信息</button></div>";
	html = html + "<div class='divBtn'><button type='button' class='btnEdit' onclick='YZYZ.Admin.Region.deleteConfirm("+record.get('id')+")'>删除</button></div>";
	html = html + "</div>"
	return html;
};

YZYZ.Admin.Region.columnModel=new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
            {header: '名称', width: 300, sortable: false, hideable:false, dataIndex: 'name'},
            {header: '显示级别', width: 100, sortable: false, hideable:false, dataIndex: 'level'},
            {header: '操作',id:'operations', width: 250, sortable: false, hideable:false, dataIndex: 'available', renderer:YZYZ.Admin.Region.opRender}
    	]);
YZYZ.Admin.Region.addNewRegion = function(){
	if(!YZYZ.Admin.Region.newRegionForm){
		YZYZ.Admin.Region.addCB = new Ext.form.ComboBox({  
	         id: 'addLevel', 
	         name:'addLevel', 
	         allowBlank: false,  
	         fieldLabel: "显示级别",  
	         hiddenName: 'hidAddLevel',  
	         displayField: 'name',  
	         valueField: 'id',  
	         typeAhead: true,  
	         triggerAction: 'all',  
	         store: YZYZ.levelData.levelStore,  
	         mode: 'local',
	         width:200 ,
	         editable:false
 		});
		YZYZ.Admin.Region.newRegionForm = new Ext.form.FormPanel({
			labelWidth:100,
			labelAlign:'right',
			region:'center',
			bodyStyle:'padding-top:20px',
			height:200,
			width:400,
			items:[YZYZ.Admin.Region.newRegionName,
			YZYZ.Admin.Region.addCB ,{
				xtype:'textarea',
				width:200,
				id:'newDescription',
				fieldLabel:'描述',
				maxLength:100
			}]
		});
		YZYZ.Admin.Region.newRegionWin = new Ext.Window({
			title:'添加分组',
			width:400,
			height:250,
			layout:'border',
			modal:true,
			closable:true,
			closeAction:'hide',
			items:[YZYZ.Admin.Region.newRegionForm],
			buttons:[{
				text:'提交',
				handler:YZYZ.Admin.Region.addSubmit
			},{
				text:'取消',
				handler:function(){YZYZ.Admin.Region.newRegionWin.hide();}
			}]
			
		});
	}
	YZYZ.Admin.Region.newRegionWin.show();
};

YZYZ.Admin.Region.addSubmit = function(){	var name = Ext.getCmp("newRegionName").getValue();
	var description = Ext.getCmp("newDescription").getValue();
	var level = Ext.getCmp("addLevel").getValue();
	if(Ext.isEmpty(name)){
		YZYZ.Curtain.alert("请填写名称");
		Ext.getCmp("newRegionName").markInvalid();
		return;
	}
	if(name.length > 20) {
		YZYZ.Curtain.alert("名称长度不能超过20");
		Ext.getCmp("newRegionName").markInvalid();
		return;
	}
	if(description.length > 100){
		YZYZ.Curtain.alert("描述的内容长度不能超过100");
		Ext.getCmp("newDescription").markInvalid();
		return;
	}
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("addUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			name 		:  name,
			description	: description,
			level		: level
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				var rec = new YZYZ.Admin.Region.newRecord({
					name:name,
					description:description,
					id:result.id,
					available:true,
					level:parseInt(level)
				});
				YZYZ.Curtain.msg("添加成功");
				YZYZ.Admin.Region.gridPanel.getStore().insert(0,rec);	
				YZYZ.Admin.Region.newRegionWin.hide();			
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序错误或网络故障");
		}
	});	
};
YZYZ.Admin.Region.init = function(){
	YZYZ.Admin.Region.newRegionName = new Ext.form.TextField({
		width:200,
		id:'newRegionName',
		fieldLabel:'名称',
		maxLength:20
	});
	YZYZ.Admin.Region.newRegionName.on("render",YZYZ.appendRedStar);
	YZYZ.Admin.Region.gridPanel = new Ext.grid.GridPanel({
			id 			: 'regionGrid',
			region		: 'center',
			height		: Ext.getBody().getHeight()-100,
			width		: Ext.getBody().getWidth(),
			store 		: YZYZ.Admin.Region.store,
			sm			: new Ext.grid.RowSelectionModel(),
			cm 			: YZYZ.Admin.Region.columnModel,
			viewConfig	: { forceFit:true },
			autoExpandColumn : 'operations',
			loadMask	: { msg:"数据加载中....." },
			bbar		: new Ext.PagingToolbar({   
				pageSize	: 15,   
				store		: YZYZ.Admin.Region.store,   
				displayInfo	: true,   
				displayMsg	: "第{0}条到{1}条  共{2}条",   
				emptyMsg	: "没有地区",
				items		:[{text:'添加地区',handler:YZYZ.Admin.Region.addNewRegion}]
			})   
	});
	
	new Ext.Viewport({
		layout: 'border',
		items:[{
			region			: 'center',
			cls				: 'divGrid',
			items 			: [YZYZ.Admin.Region.gridPanel],
			autoScroll		: false,
			layout			: 'fit' 
		}]
	});
	YZYZ.Admin.Region.store.load({params:{start:0,limit:15}});
};
Ext.onReady(YZYZ.Admin.Region.init, document);
